using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using StockDownloader;
using Stocks;
using StockDownloader.Persistance;
namespace StockAnalyze
{
    public partial class WealthPortfolioManager : Form
    {
        DataPersitance _dp = new DataPersitance();
        public WealthPortfolioManager()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this._dp.OpenDatabase(null,null);
            PopulateTree();
           
        }
        private void PopulateTree()
        {
            StockLog[] slog = this._dp.GetStoredStockList();
            this.tvStocks.Nodes.Clear();
            if (slog != null)
            {
                TreeNode tn;
                foreach (StockLog sl in slog)
                {
                    tn = new TreeNode();
                    tn.Text = sl.Symbol;
                    tn.Tag = sl;
                    this.tvStocks.Nodes.Add(tn);
                }
            }
        }


        private void addSymbolToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void tsbAddStock_Click(object sender, EventArgs e)
        {
            if (this.tstbSymbol.Text.Length == 0)
            {
                MessageBox.Show("Please enter a Symbol");
                this.tstbSymbol.BackColor = Color.LightYellow;
            }
            else
            {
                try
                {
                    StockInstanceDataCollectionEx ex = YahooAccessor.GetHistoricData(this.tstbSymbol.Text, DateTime.Now.Subtract(new TimeSpan(60)), DateTime.Now);
                    StockLog sl = new StockLog();
                    sl.StockInstantDataCollection = ex;
                    sl.Name = ex.Symbol;
                    sl.Symbol = ex.Symbol;
                    if (ex != null)
                    {
                        DataPersitance.SaveStockLog(sl, ex.Symbol);
                        this.PopulateTree();
                    }
                }
                catch (System.Net.WebException err)
                {
                    MessageBox.Show("Stock symbol not found. Erro information : " + err.Message);
                }

            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            this._dp.Dispose();
        }

        private void tvStocks_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.pgStocks.SelectedObject = this.tvStocks.SelectedNode.Tag;
            Stocks.StockLog sItem =  (Stocks.StockLog)e.Node.Tag;
            
            StockInstanceDataCollectionEx InstanceLog = DataPersitance.GetStockLogStatic(sItem.Symbol,ctrlStockDataView.StartDate,ctrlStockDataView.EndDate);
            lbMessages.Items.Add(string.Format("Located {0} records matching the dates specified",InstanceLog.Count));
        }

        private void stockGraph1_Load(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void indexDataToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataPersitance.IndexAll();
        }

        private void updatePricesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // foreach stock
            foreach (TreeNode tn in this.tvStocks.Nodes)
            {
                // get latest price date from db

                // download data from yahoo
                // update db prices
            }
        }

        private void getEODDataToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //StockDownloader.Accessors.EODDataAccessor.GetLatestMissingEodFiles("", new DateTime(2006, 1, 1), "NASDAQ");
        }
        
    }
}